package com.maiko.usercenter.common;

import lombok.Data;

import java.io.Serializable;

/**
 * 通用返回类，用于封装后端接口统一响应结构
 * @param <T> 响应数据类型
 *
 * @author: Maiko7
 * @create: 2025-07-13 15:23
 */
@Data
public class BaseResponse<T> implements Serializable {

    private static final long serialVersionUID = -1L;

    /**
     * 状态码
     */
    private int code;

    /**
     * 响应数据
     */
    private T data;

    /**
     * 提示信息
     */
    private String message;

    /**
     * 错误详情描述
     */
    private String description;

    // 全参构造
    public BaseResponse(int code, T data, String message, String description) {
        this.code = code;
        this.data = data;
        this.message = message;
        this.description = description;
    }

    /**
     * 构造：成功 + 数据
     *
     * {
     *   "code": 200,
     *   "message": "注册成功",
     *   "data": {
     *     "id": 1,
     *     "username": "maiko"
     *   },
     *   "description": ""
     * }
     */
    public BaseResponse(int code, T data, String message) {
        this(code, data, message, "");
    }

    /**
     * 构造：成功 + 数据 + 提示信息
     *
     * {
     *   "code": 200,
     *   "message": "",
     *   "data": [
     *     {"id": 1, "username": "maiko"},
     *     {"id": 2, "username": "lily"}
     *   ],
     *   "description": ""
     * }
     */
    public BaseResponse(int code, T data) {
        this(code, data, "", "");
    }

    /**
     * 构造：仅错误码
     *
     * {
     *   "code": 40000,
     *   "message": "请求参数错误",
     *   "data": null,
     *   "description": ""
     * }
     */
    public BaseResponse(ErrorCode errorCode) {
        this(errorCode.getCode(), null, errorCode.getMessage(), errorCode.getDescription());
    }


    /**
     * 构造：错误码 + 自定义描述
     *
     * {
     *   "code": 40101,
     *   "message": "用户不存在",
     *   "data": null,
     *   "description": "用户ID: 88 不存在"
     * }
     */
    public BaseResponse(ErrorCode errorCode, String description) {
        this(errorCode.getCode(), null, errorCode.getMessage(), description);
    }

    /**
     * 构造：错误码 + 自定义消息 + 自定义描述
     *
     * {
     *   "code": 40203,
     *   "message": "两次密码不一致",
     *   "data": null,
     *   "description": "注册页面校验失败"
     * }
     */
    public BaseResponse(ErrorCode errorCode, String message, String description) {
        this(errorCode.getCode(), null, message, description);
    }
}
